package com.lbs.tdrive;


import com.lbs.tdrive.R;
import com.lbs.tdrive.comon.HttpRequest;
import com.lbs.tdrive.constant.AppConstant;
import com.lbs.tdrive.home.HomeScreen;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener{
	private Button login;
	private Button cancel;
    private EditText username;
    private EditText password;
    private TextView register;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
          }
        login=(Button)findViewById(R.id.button1);
        cancel=(Button)findViewById(R.id.button2);
    	username=(EditText)findViewById(R.id.editText1);
    	password=(EditText)findViewById(R.id.editText2);
    	register = (TextView) findViewById(R.id.textView4);
    	login.setOnClickListener(this);
    	cancel.setOnClickListener(this);
    	register.setOnClickListener(this);
    }
  
    public void onClick(View v) {
    	if(v.getId()==R.id.button1){
    	 String user = username.getText().toString();
    	 String pass = password.getText().toString();
    	 if (user.equals("admin") && pass.equals("admin")) {
    		 boolean isLogin = checkAuthentication(user, pass);
    		 if(isLogin){
    			 Toast.makeText(LoginActivity.this,"Login Successful!!!", Toast.LENGTH_SHORT).show();
    			 SharedPreferences pref = getSharedPreferences(AppConstant.PREFS_NAME,0);
 				SharedPreferences.Editor editor = pref.edit();
 				editor.putString("username", user);
 				editor.commit();
    	    	  Intent intent = new Intent(this,HomeScreen.class);
    	    	  startActivity(intent);
    	    	  finish();
    		 }else {
    			 Toast.makeText(LoginActivity.this,"Wrong Username or Password!!!", Toast.LENGTH_SHORT).show();	
    		 }
    				 
    	 }
    	 else {
    	  username.setText("");
    	  password.setText("");
    	  Toast.makeText(LoginActivity.this,"Wrong Username or Password!!!", Toast.LENGTH_SHORT).show();	
    	 }
    	}
    	else if(v.getId()==R.id.button2){
    		finish();
    	} 
    	else if(v.getId()==R.id.textView4){
    		Intent intent = new Intent(this, RegisterActivity.class);
    		startActivity(intent);	
    	}
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.login, menu);
        return true;
    }

	private boolean checkAuthentication(String userName,String password) {
		System.out.println("username="+userName);
		System.out.println("password="+password);
		String loginPath=AppConstant.LOGIN_PATH+"?username="+userName+"&password="+password;
		System.out.println("loginpath="+loginPath);
		String respStr=HttpRequest.getResponse(loginPath);
			if(respStr.contains("true")) {
				return true;
			}
			
	
			
		
		return false;	
	}
    
}
